package util;

import java.lang.reflect.*;
import java.util.HashMap;
//import com.google.gson.Gson;

public class Executor {

    public static HashMap<String, String> getParams(String parameters) {
        return getParams(parameters, "&", "=");
    }

    public static HashMap<String, String> getOParams(String parameters) {
        return getParams(parameters, ",", ":");
    }

	public static HashMap<String, String> getParams(String parameters, String sep, String igual) {
		HashMap<String, String> prms = new HashMap<String, String>();
		
		String[] params = parameters.split(sep);
		int np = params.length, p;

        //Gson gson = new Gson();
		for (p = 0; p < np; p ++) {
			String param = params[p];
			
			if (param.indexOf(igual) != -1) {
				String[] pairs = param.split(igual);
			
                if (pairs.length > 1) prms.put(pairs[0], pairs[1]);
                else prms.put(pairs[0], "");
			}	else prms.put(param, "");
		}
		
		return prms;
	}
	
	public static Object exec(String className, String methodName, String prms) {
		Class<?> clase;
		Object object;
		Method method;
		Object obj = "";
		
		Class<?>[] args = null;
		Object[] vals = null;
		
		if (!prms.equals("")) {
			String params[] = prms.split("&");
			System.out.println("np: " + params.length);
			System.out.println("val: " + params[0]);
			
			args = new Class[params.length];
			vals = new Object[params.length];
			int p;
			
			for (p = 0; p < params.length; p ++) {
				System.out.println("param:" + params[p]);
				
				String[] pairs = params[p].split("=");
				String type = pairs[0];
				String val = ""; 
				if (pairs.length > 1) val= pairs[1];
				
				type = type.toLowerCase();
				if (type.equals("string")) {
					args[p] = String.class;
					if (val.equals("null")) val = null;
					vals[p] = val;
				}	else if (type.equals("byte")) {
					args[p] = byte.class;
					vals[p] = new Byte(val);
				}	else if (type.equals("int")) {
					args[p] = int.class;
					vals[p] = Integer.parseInt(val);
				}	else if (type.equals("long")) {
					args[p] = long.class;
					vals[p] = Long.parseLong(val);
				}	else if (type.equals("float")) {
					args[p] = float.class;
					vals[p] = Float.parseFloat(val);
				}	else if (type.equals("boolean")) {
					args[p] = boolean.class;
					vals[p] = Boolean.parseBoolean(val);
				}
			}
		}
		
		try {
			clase = Class.forName(className);
			try {
				object = clase.newInstance();
				try {
					method = clase.getMethod(methodName, args);
					obj = method.invoke(object, vals); 
					
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		return obj;
	}
}